package com.example.sportgoods.controller;

import com.example.sportgoods.domain.Goods;
import com.example.sportgoods.Service.GoodsService;
import com.example.sportgoods.util.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "商品管理（0528王雪伊）")
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    // 获取所有商品列表的接口
    @ApiOperation(value = "取得所有商品列表", notes = "取得所有商品列表")
    @GetMapping("/list")
    public AjaxResult list() {
        List<Goods> allGoods = goodsService.selectAllGoods();
        return AjaxResult.success(allGoods);
    }

    // 根据商品id查询商品详情的接口
    @ApiOperation(value = "根据id查询商品详情", notes = "根据id查询商品详情")
    @ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(goodsService.selectGoodsById(id));
    }

    // 修改商品信息的接口
    @ApiOperation("修改商品信息")
    @PutMapping
    public AjaxResult edit(@RequestBody Goods goods) {
        return AjaxResult.success(goodsService.updateGoods(goods));
    }

    // 新增商品的接口
    @ApiOperation("新增商品")
    @PostMapping
    public AjaxResult add(@RequestBody Goods goods) {
        goodsService.addGoods(goods);
        return AjaxResult.success("商品新增成功");
    }

    // 根据商品id删除商品的接口
    @ApiOperation("根据id删除商品")
    @ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        goodsService.deleteGoodsById(id);
        return AjaxResult.success("商品删除成功");
    }
}